package gamepad.vistas;

import gamepad.Controlador;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import mobilefeel.AndroidDevice;

import org.flypad.joystick.JoystickException;




public class VistaPrincipal extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;

	private Controlador controlador;
	private AndroidDevice[] devices;
	private List<AndroidDevice> joysticks;
	
	public VistaPrincipal(Controlador c) {
		controlador = c;
		devices = new AndroidDevice[16];
		joysticks = new ArrayList<AndroidDevice>();
		
		try {
        	UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
		
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lista = new javax.swing.JList();
        configurarButton = new javax.swing.JButton();
        desconectarButton = new javax.swing.JButton();
        subirButton = new javax.swing.JButton();
        bajarButton = new javax.swing.JButton();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusLabel = new javax.swing.JLabel();
        statusTimer = new javax.swing.Timer(5000, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				statusLabel.setText(" ");
			}
		});
        herramientasToolBar = new javax.swing.JToolBar();
        iniciarButton = new javax.swing.JButton();
        detenerButton = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        contenidoButton = new javax.swing.JButton();
        acercaDeButton = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu archivoMenu = new javax.swing.JMenu();
        iniciarMenuItem = new javax.swing.JMenuItem();
        detenerMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        javax.swing.JMenuItem salirMenuItem = new javax.swing.JMenuItem();
        verMenu = new javax.swing.JMenu();
        mostrarBarraHerramientasmenuItem = new javax.swing.JCheckBoxMenuItem();
        mostrarBarraEstadoMenuItem = new javax.swing.JCheckBoxMenuItem();
        javax.swing.JMenu ayudaMenu = new javax.swing.JMenu();
        contenidoMenuItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        acercaDeMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("GamePad");
        setResizable(false);
        addWindowListener(new WindowListener() {
			public void windowOpened(WindowEvent e) {}
			public void windowIconified(WindowEvent e) {}
			public void windowDeiconified(WindowEvent e) {}
			public void windowDeactivated(WindowEvent e) {}
			public void windowClosing(WindowEvent e) {
				try {
					controlador.doSalir();
				} catch (IOException e1) {}
			}
			public void windowClosed(WindowEvent e) {}
			public void windowActivated(WindowEvent e) {}
		});
        
        lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lista.setModel(getModel());
        lista.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				listaSelectionValueChanged(e);
			}
		});
        jScrollPane1.setViewportView(lista);

        configurarButton.setText("Configurar");
        configurarButton.setEnabled(false);
        configurarButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				configurarButtonActionPerformed(e);
			}
		});
        
        desconectarButton.setText("Desconectar");
        desconectarButton.setEnabled(false);
        desconectarButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desconectarButtonActionPerformed(e);
			}
		});
        
        subirButton.setText("Subir");
        subirButton.setEnabled(false);
        subirButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				subirButtonActionPerformed(e);
			}
		});
        
        bajarButton.setText("Bajar");
        bajarButton.setEnabled(false);
        bajarButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bajarButtonActionPerformed(e);
			}
		});
        
        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(configurarButton, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                    .addComponent(desconectarButton, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                    .addComponent(subirButton, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                    .addComponent(bajarButton, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(configurarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25)
                        .addComponent(subirButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bajarButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                        .addComponent(desconectarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        statusLabel.setText("GamePad");
        statusTimer.setRepeats(false);

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusLabel)
                .addContainerGap(404, Short.MAX_VALUE))
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE)
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusLabel)
                .addContainerGap(11, Short.MAX_VALUE))
        );

        herramientasToolBar.setRollover(true);

        iniciarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gamepad/vistas/iconos/31.png"))); // NOI18N
        iniciarButton.setToolTipText("Iniciar Servidor");
        iniciarButton.setFocusable(false);
        iniciarButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        iniciarButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        iniciarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                iniciarButtonActionPerformed(evt);
            }
        });
        herramientasToolBar.add(iniciarButton);

        detenerButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gamepad/vistas/iconos/42.png"))); // NOI18N
        detenerButton.setToolTipText("Detener Servidor");
        detenerButton.setFocusable(false);
        detenerButton.setVisible(false);
        detenerButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        detenerButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        detenerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                detenerButtonActionPerformed(evt);
            }
        });
        herramientasToolBar.add(detenerButton);

        herramientasToolBar.add(jSeparator3);

        contenidoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gamepad/vistas/iconos/15.png"))); // NOI18N
        contenidoButton.setToolTipText("Contenido");
        contenidoButton.setFocusable(false);
        contenidoButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        contenidoButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        contenidoButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				contenidoButtonActionPerformed(evt);
			}
		});
        herramientasToolBar.add(contenidoButton);

        acercaDeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gamepad/vistas/iconos/100.png"))); // NOI18N
        acercaDeButton.setToolTipText("Acerca de...");
        acercaDeButton.setFocusable(false);
        acercaDeButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        acercaDeButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        acercaDeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acercaDeButtonActionPerformed(evt);
            }
        });
        herramientasToolBar.add(acercaDeButton);

        archivoMenu.setText("Archivo");

        iniciarMenuItem.setText("Iniciar Servidor");
        iniciarMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				iniciarMenuItemActionPerformed(e);
			}
		});
        archivoMenu.add(iniciarMenuItem);

        detenerMenuItem.setText("Detener Servidor");
        detenerMenuItem.setEnabled(false);
        detenerMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				detenerMenuItemActionPerformed(e);
			}
		});
        archivoMenu.add(detenerMenuItem);

        archivoMenu.add(jSeparator1);

        salirMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        salirMenuItem.setText("Salir");
        salirMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				salirMenuItemActionPerformed(e);
			}
		});
        archivoMenu.add(salirMenuItem);

        menuBar.add(archivoMenu);

        verMenu.setText("Ver");

        mostrarBarraHerramientasmenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        mostrarBarraHerramientasmenuItem.setSelected(true);
        mostrarBarraHerramientasmenuItem.setText("Mostrar Barra Herramientas");
        mostrarBarraHerramientasmenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				herramientasCheckBoxMenuItemActionPerformed(e);
			}
		});
        verMenu.add(mostrarBarraHerramientasmenuItem);

        mostrarBarraEstadoMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        mostrarBarraEstadoMenuItem.setSelected(true);
        mostrarBarraEstadoMenuItem.setText("Mostrar Barra Estado");
        mostrarBarraEstadoMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				estadoCheckBoxMenuItemActionPerformed(e);
			}
		});
        verMenu.add(mostrarBarraEstadoMenuItem);

        menuBar.add(verMenu);

        ayudaMenu.setText("Ayuda");

        contenidoMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        contenidoMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gamepad/vistas/iconos/15.png"))); // NOI18N
        contenidoMenuItem.setText("Contenido");
        contenidoMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				contenidoMenuItemActionPerformed(e);
			}
		});
        ayudaMenu.add(contenidoMenuItem);

        ayudaMenu.add(jSeparator2);

        acercaDeMenuItem.setText("Acerca de...");
        acercaDeMenuItem.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
				acercaDeMenuItemActionPerformed(e);
			}
		});
        ayudaMenu.add(acercaDeMenuItem);

        menuBar.add(ayudaMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(herramientasToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE)
            .addComponent(statusPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(herramientasToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 222, Short.MAX_VALUE)
                .addComponent(statusPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(30, 30, 30)
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(42, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	public int agregarDispositivo(AndroidDevice device) {
    	int joystick = 0;
    	for (int i = 0; i < devices.length; i++) {
    		if (devices[i] == null) {
    			devices[i] = device;
    			joysticks.add(i, device);
    			joystick = i + 1;
    			break;
    		}
    	}
    	return joystick;
    }
    
    public void desconectarDispositivo(AndroidDevice device) {
    	for (int i = 0; i < devices.length; i++) {
    		if (devices[i] == device) {
    			devices[i] = null;
    			joysticks.remove(device);
    			break;
    		}
    	}
    	actualizarLista();
    }
    
    public void actualizarLista() {
    	lista.setModel(getModel());
    }
    
    public ListModel getModel() {
    	return new javax.swing.AbstractListModel() {
			private static final long serialVersionUID = 1L;
            public int getSize() { return joysticks.size(); }
            public Object getElementAt(int i) { return "Joystick " + controlador.getJoystickNum(joysticks.get(i)) + ": " + joysticks.get(i).toString(); }
        };
    }
    
    public void servidorIniciado() {
        iniciarMenuItem.setEnabled(!iniciarMenuItem.isEnabled());
        detenerMenuItem.setEnabled(!detenerMenuItem.isEnabled());
        iniciarButton.setVisible(!iniciarButton.isVisible());
        detenerButton.setVisible(!detenerButton.isVisible());
    }
    
    public void setEstado(String estado) {
        statusLabel.setText(estado);
        statusTimer.restart();
    }
    
    public void errorCrearJoystick(AndroidDevice device) {
    	desconectarDispositivo(device);
    	JOptionPane.showMessageDialog(this, "Error al crear Joystick. Revise si tiene disponible un Joystick Virtual de PPJoy.", "Error", JOptionPane.WARNING_MESSAGE);
    }
    
    private void herramientasCheckBoxMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_herramientasCheckBoxMenuItemActionPerformed
        herramientasToolBar.setVisible(!herramientasToolBar.isVisible());
    }//GEN-LAST:event_herramientasCheckBoxMenuItemActionPerformed

    private void estadoCheckBoxMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_estadoCheckBoxMenuItemActionPerformed
        statusPanel.setVisible(!statusPanel.isVisible());
    }//GEN-LAST:event_estadoCheckBoxMenuItemActionPerformed

    private void iniciarMenuItemActionPerformed(ActionEvent evt) {
    	int puerto = 51460;
    	try {
    		Object puertoStr = JOptionPane.showInputDialog(this, "Ingrese puerto:", "GamePad", JOptionPane.PLAIN_MESSAGE, null, null, "51460");
    		if (puertoStr != null) {
	    		puerto = Integer.parseInt(puertoStr.toString());
	    		controlador.doIniciar(puerto);
	        	servidorIniciado();
    		}
    	} catch (NumberFormatException e) {
    		JOptionPane.showMessageDialog(this, "Puerto no válido.", "Error", JOptionPane.WARNING_MESSAGE);
    	} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Error al abrir socket.", "Error", JOptionPane.WARNING_MESSAGE);
		}	
    }
    
    private void detenerMenuItemActionPerformed(ActionEvent evt) {
    	try {
	    	controlador.doDetener();
	    	servidorIniciado();
    	} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Error al cerrar socket.", "Error", JOptionPane.WARNING_MESSAGE);
		}
    }
    
    private void contenidoMenuItemActionPerformed(ActionEvent evt) {
    	if (Desktop.isDesktopSupported()) {
			try {
				Desktop.getDesktop().open(new File(System.getProperty("user.dir") + "/manual/gamepad.html"));
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, "Error al abrir Contenido.", "Error", JOptionPane.WARNING_MESSAGE);
			}
    	} else
    		JOptionPane.showMessageDialog(this, "Error al abrir Contenido.", "Error", JOptionPane.WARNING_MESSAGE);
    }
    
    private void acercaDeMenuItemActionPerformed(ActionEvent evt) {
    	JOptionPane.showMessageDialog(this, "GamePad\nMobileFeel SDK for Android\nProyecto Final de Ingeniería UADE\n\n-Gschwind, Nicolás Darío\n-Smirnoff, Christian Alejandro\n\n2011 Todos los derechos reservados", "Acerca de GamePad", JOptionPane.INFORMATION_MESSAGE);
    }

	private void salirMenuItemActionPerformed(ActionEvent evt) {
		try {
			controlador.doSalir();
		} catch (IOException e) {
			System.exit(1);
		}
		System.exit(0);
	}
    
	private void configurarButtonActionPerformed(ActionEvent evt) {
		int index = lista.getSelectedIndex();
		if (index != -1) {
			controlador.doConfigurar(joysticks.get(index));
		}
	}
	
	private void desconectarButtonActionPerformed(ActionEvent evt) {
		int index = lista.getSelectedIndex();
		if (index != -1) {
			try {
				controlador.doDesconectar(joysticks.get(index));
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, "Error al desconectar.", "Error", JOptionPane.WARNING_MESSAGE);
			}
		}
	}

	private void subirButtonActionPerformed(ActionEvent evt) {
		int index = lista.getSelectedIndex();
		if (index > 0) {
			try {
				controlador.doIntercambiar(joysticks.get(index - 1), joysticks.get(index));
			} catch (JoystickException e) {
				JOptionPane.showMessageDialog(this, "No se pudo realizar la operación solicitada.", "Error", JOptionPane.WARNING_MESSAGE);
			}
		}
	}
	
	private void bajarButtonActionPerformed(ActionEvent evt) {
		int index = lista.getSelectedIndex();
		if (index >= 0 && index < joysticks.size() - 1) {
			try {
				controlador.doIntercambiar(joysticks.get(index), joysticks.get(index + 1));
			} catch (JoystickException e) {
				JOptionPane.showMessageDialog(this, "No se pudo realizar la operación solicitada.", "Error", JOptionPane.WARNING_MESSAGE);
			}
		}
	}
	
	private void iniciarButtonActionPerformed(ActionEvent evt) {
		iniciarMenuItem.doClick();
	}
	
	private void detenerButtonActionPerformed(ActionEvent evt) {
		detenerMenuItem.doClick();
	}
	
	private void contenidoButtonActionPerformed(ActionEvent evt) {
		contenidoMenuItem.doClick();
	}
	
	private void acercaDeButtonActionPerformed(ActionEvent evt) {
		acercaDeMenuItem.doClick();
	}

    protected void listaSelectionValueChanged(ListSelectionEvent e) {
		int index = lista.getSelectedIndex();
		if (index == -1) {
			configurarButton.setEnabled(false);
			subirButton.setEnabled(false);
			bajarButton.setEnabled(false);
			desconectarButton.setEnabled(false);
		} else {
			configurarButton.setEnabled(true);
			if (index > 0)
				subirButton.setEnabled(true);
			else
				subirButton.setEnabled(false);
			if (index < joysticks.size() - 1)
				bajarButton.setEnabled(true);
			else
				bajarButton.setEnabled(false);
			desconectarButton.setEnabled(true);
		}
	}
	
    public void errorGuardarXml() {
		JOptionPane.showMessageDialog(this, "Error al guardar configuración del dispositivo", "Error", JOptionPane.WARNING_MESSAGE);
	}
	
	public void errorCargarXml() {
		JOptionPane.showMessageDialog(this, "Error al leer configuración del dispositivo", "Error", JOptionPane.WARNING_MESSAGE);
	}

	public void errorConexion() {
		JOptionPane.showMessageDialog(this, "Error de comunicación con el dispositivo", "Error", JOptionPane.WARNING_MESSAGE);
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem acercaDeMenuItem;
	private javax.swing.JButton acercaDeButton;
    private javax.swing.JButton bajarButton;
    private javax.swing.JButton configurarButton;
    private javax.swing.JButton contenidoButton;
    private javax.swing.JMenuItem contenidoMenuItem;
    private javax.swing.JButton desconectarButton;
    private javax.swing.JButton detenerButton;
    private javax.swing.JMenuItem detenerMenuItem;
    private javax.swing.JButton iniciarButton;
    private javax.swing.JMenuItem iniciarMenuItem;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar herramientasToolBar;
    private javax.swing.JList lista;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JCheckBoxMenuItem mostrarBarraEstadoMenuItem;
    private javax.swing.JCheckBoxMenuItem mostrarBarraHerramientasmenuItem;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.Timer statusTimer;
    private javax.swing.JButton subirButton;
    private javax.swing.JMenu verMenu;
    // End of variables declaration//GEN-END:variables

}
